home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / as / dist / atof-m68k.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-01  |  13.2 KB  |  492 lines

  1. /* atof_m68k.c - turn a Flonum into a 68020 floating point number
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "flonum.h"
  21. #ifdef USG
  22. #define bzero(s,n) memset(s,0,n)
  23. #endif
  24.  
  25. extern FLONUM_TYPE generic_floating_point_number; /* Flonums returned here. */
  26. #define NULL (0)
  27.  
  28. extern char EXP_CHARS[];
  29.                 /* Precision in LittleNums. */
  30. #define MAX_PRECISION (6)
  31. #define F_PRECISION (2)
  32. #define D_PRECISION (4)
  33. #define X_PRECISION (6)
  34. #define P_PRECISION (6)
  35.  
  36.                 /* Length in LittleNums of guard bits. */
  37. #define GUARD (2)
  38.  
  39. static unsigned long int mask [] = {
  40.   0x00000000,
  41.   0x00000001,
  42.   0x00000003,
  43.   0x00000007,
  44.   0x0000000f,
  45.   0x0000001f,
  46.   0x0000003f,
  47.   0x0000007f,
  48.   0x000000ff,
  49.   0x000001ff,
  50.   0x000003ff,
  51.   0x000007ff,
  52.   0x00000fff,
  53.   0x00001fff,
  54.   0x00003fff,
  55.   0x00007fff,
  56.   0x0000ffff,
  57.   0x0001ffff,
  58.   0x0003ffff,
  59.   0x0007ffff,
  60.   0x000fffff,
  61.   0x001fffff,
  62.   0x003fffff,
  63.   0x007fffff,
  64.   0x00ffffff,
  65.   0x01ffffff,
  66.   0x03ffffff,
  67.   0x07ffffff,
  68.   0x0fffffff,
  69.   0x1fffffff,
  70.   0x3fffffff,
  71.   0x7fffffff,
  72.   0xffffffff
  73.   };
  74.  
  75. static int bits_left_in_littlenum;
  76. static int littlenums_left;
  77. static LITTLENUM_TYPE *    littlenum_pointer;
  78.  
  79. static int
  80. next_bits (number_of_bits)
  81.      int        number_of_bits;
  82. {
  83.   int            return_value;
  84.  
  85.   if(!littlenums_left)
  86.       return 0;
  87.   if (number_of_bits >= bits_left_in_littlenum)
  88.     {
  89.       return_value  = mask [bits_left_in_littlenum] & *littlenum_pointer;
  90.       number_of_bits -= bits_left_in_littlenum;
  91.       return_value <<= number_of_bits;
  92.       if(--littlenums_left) {
  93.           bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  94.           littlenum_pointer --;
  95.           return_value |= (*littlenum_pointer>>bits_left_in_littlenum) & mask[number_of_bits];
  96.       }
  97.     }
  98.   else
  99.     {
  100.       bits_left_in_littlenum -= number_of_bits;
  101.       return_value = mask [number_of_bits] & (*littlenum_pointer>>bits_left_in_littlenum);
  102.     }
  103.   return (return_value);
  104. }
  105.  
  106. /* Num had better be less than LITTLENUM_NUMBER_OF_BITS */
  107. static int
  108. unget_bits(num)
  109. {
  110.     if(!littlenums_left) {
  111.         ++littlenum_pointer;
  112.         ++littlenums_left;
  113.         bits_left_in_littlenum=num;
  114.     } else if(bits_left_in_littlenum+num>LITTLENUM_NUMBER_OF_BITS) {
  115.         bits_left_in_littlenum= num-(LITTLENUM_NUMBER_OF_BITS-bits_left_in_littlenum);
  116.         ++littlenum_pointer;
  117.         ++littlenums_left;
  118.     } else
  119.         bits_left_in_littlenum+=num;
  120. }
  121.  
  122. static void
  123. make_invalid_floating_point_number (words)
  124.      LITTLENUM_TYPE *    words;
  125. {
  126.     as_warn("cannot create floating-point number");
  127.     words[0]= ((unsigned)-1)>>1;    /* Zero the leftmost bit */
  128.     words[1]= -1;
  129.     words[2]= -1;
  130.     words[3]= -1;
  131.     words[4]= -1;
  132.     words[5]= -1;
  133. }
  134.  
  135. /***********************************************************************\
  136. *    Warning: this returns 16-bit LITTLENUMs. It is up to the caller    *
  137. *    to figure out any alignment problems and to conspire for the    *
  138. *    bytes/word to be emitted in the right order. Bigendians beware!    *
  139. *                                    *
  140. \***********************************************************************/
  141.  
  142. char *                /* Return pointer past text consumed. */
  143. atof_m68k (str, what_kind, words)
  144.      char *        str;    /* Text to convert to binary. */
  145.      char        what_kind; /* 'd', 'f', 'g', 'h' */
  146.      LITTLENUM_TYPE *    words;    /* Build the binary here. */
  147. {
  148.     LITTLENUM_TYPE    bits [MAX_PRECISION + MAX_PRECISION + GUARD];
  149.                 /* Extra bits for zeroed low-order bits. */
  150.                 /* The 1st MAX_PRECISION are zeroed, */
  151.                 /* the last contain flonum bits. */
  152.     char *        return_value;
  153.     int        precision; /* Number of 16-bit words in the format. */
  154.     long int    exponent_bits;
  155.  
  156.     return_value = str;
  157.     generic_floating_point_number.low    = bits + MAX_PRECISION;
  158.     generic_floating_point_number.high    = NULL;
  159.     generic_floating_point_number.leader    = NULL;
  160.     generic_floating_point_number.exponent    = NULL;
  161.     generic_floating_point_number.sign    = '\0';
  162.  
  163.                 /* Use more LittleNums than seems */
  164.                 /* necessary: the highest flonum may have */
  165.                 /* 15 leading 0 bits, so could be useless. */
  166.  
  167.     bzero (bits, sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
  168.  
  169.     switch(what_kind) {
  170.     case 'f':
  171.     case 'F':
  172.     case 's':
  173.     case 'S':
  174.         precision = F_PRECISION;
  175.         exponent_bits = 8;
  176.         break;
  177.  
  178.     case 'd':
  179.     case 'D':
  180.     case 'r':
  181.     case 'R':
  182.         precision = D_PRECISION;
  183.         exponent_bits = 11;
  184.         break;
  185.  
  186.     case 'x':
  187.     case 'X':
  188.     case 'e':
  189.     case 'E':
  190.         precision = X_PRECISION;
  191.         exponent_bits = 15;
  192.         break;
  193.  
  194.     case 'p':
  195.     case 'P':
  196.         
  197.         precision = P_PRECISION;
  198.         exponent_bits= -1;
  199.         break;
  200.  
  201.     default:
  202.         make_invalid_floating_point_number (words);
  203.         return NULL;
  204.     }
  205.  
  206.     generic_floating_point_number.high = generic_floating_point_number.low + precision - 1 + GUARD;
  207.  
  208.     if (atof_generic (& return_value, ".", EXP_CHARS, & generic_floating_point_number)) {
  209.         /* as_warn("Error converting floating point number (Exponent overflow?)"); */
  210.         make_invalid_floating_point_number (words);
  211.         return NULL;
  212.     }
  213.     gen_to_words(words, precision, exponent_bits);
  214.     return return_value;
  215. }
  216.  
  217. char *
  218. print_gen(gen)
  219. FLONUM_TYPE *gen;
  220. {
  221.     FLONUM_TYPE f;
  222.     LITTLENUM_TYPE arr[10];
  223.     double dv;
  224.     float fv;
  225.     static char sbuf[40];
  226.  
  227.     f=generic_floating_point_number;
  228.     generic_floating_point_number= *gen;
  229.     gen_to_words(&arr[0],4,11);
  230.     bcopy(&arr[0],&dv,sizeof(double));
  231.     sprintf(sbuf,"%x %x %x %x %.14G   ",arr[0],arr[1],arr[2],arr[3],dv);
  232.     gen_to_words(&arr[0],2,8);
  233.     bcopy(&arr[0],&fv,sizeof(float));
  234.     sprintf(sbuf+strlen(sbuf),"%x %x %.12g\n",arr[0],arr[1],fv);
  235.     generic_floating_point_number=f;
  236.     return sbuf;
  237. }
  238.  
  239. /* Turn generic_floating_point_number into a real float/double/extended */
  240. gen_to_words(words,precision,exponent_bits)
  241. LITTLENUM_TYPE *words;
  242. long int    exponent_bits;
  243. int precision;
  244. {
  245.     int return_value=0;
  246.  
  247.     long int    exponent_1;
  248.     long int    exponent_2;
  249.     long int    exponent_3;
  250.     long int    exponent_4;
  251.     int        exponent_skippage;
  252.     LITTLENUM_TYPE    word1;
  253.     LITTLENUM_TYPE *    lp;
  254.  
  255.     if (generic_floating_point_number.low > generic_floating_point_number.leader) {
  256.         /* 0.0e0 seen. */
  257.         bzero (words, sizeof(LITTLENUM_TYPE) * precision);
  258.         return return_value;
  259.     }
  260.  
  261.     /* NaN:  Do the right thing */
  262.     if(generic_floating_point_number.sign==0) {
  263.         if(precision==F_PRECISION) {
  264.             words[0]=0xffc0;
  265.             words[1]=0x0007;
  266.         } else {
  267.             words[0]=0x7ff0;
  268.             words[1]=0;
  269.             words[2]=0xffff;
  270.             words[3]=0xffff;
  271.         }
  272.         return return_value;
  273.     } else if(generic_floating_point_number.sign=='P') {
  274.         /* +INF:  Do the right thing */
  275.         if(precision==F_PRECISION) {
  276.             words[0]=0x7f80;
  277.             words[1]=0;
  278.         } else {
  279.             words[0]=0x7ff0;
  280.             words[1]=0;
  281.             words[2]=0;
  282.             words[3]=0;
  283.         }
  284.         return return_value;
  285.     } else if(generic_floating_point_number.sign=='N') {
  286.         /* Negative INF */
  287.         if(precision==F_PRECISION) {
  288.             words[0]=0xff80;
  289.             words[1]=0x0;
  290.         } else {
  291.             words[0]=0xfff0;
  292.             words[1]=0x0;
  293.             words[2]=0x0;
  294.             words[3]=0x0;
  295.         }
  296.         return return_value;
  297.     }
  298.         /*
  299.          * The floating point formats we support have:
  300.          * Bit 15 is sign bit.
  301.          * Bits 14:n are excess-whatever exponent.
  302.          * Bits n-1:0 (if any) are most significant bits of fraction.
  303.          * Bits 15:0 of the next word(s) are the next most significant bits.
  304.          *
  305.          * So we need: number of bits of exponent, number of bits of
  306.          * mantissa.
  307.          */
  308.     bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  309.     littlenum_pointer = generic_floating_point_number.leader;
  310.     littlenums_left = 1+generic_floating_point_number.leader - generic_floating_point_number.low;
  311.     /* Seek (and forget) 1st significant bit */
  312.     for (exponent_skippage = 0;! next_bits(1); exponent_skippage ++)
  313.         ;
  314.     exponent_1 = generic_floating_point_number.exponent + generic_floating_point_number.leader + 1 -
  315.  generic_floating_point_number.low;
  316.     /* Radix LITTLENUM_RADIX, point just higher than generic_floating_point_number.leader. */
  317.     exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  318.     /* Radix 2. */
  319.     exponent_3 = exponent_2 - exponent_skippage;
  320.     /* Forget leading zeros, forget 1st bit. */
  321.     exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
  322.     /* Offset exponent. */
  323.  
  324.     lp = words;
  325.  
  326.     /* Word 1. Sign, exponent and perhaps high bits. */
  327.     word1 =   (generic_floating_point_number.sign == '+') ? 0 : (1<<(LITTLENUM_NUMBER_OF_BITS-1));
  328.  
  329.     /* Assume 2's complement integers. */
  330.     if(exponent_4<1 && exponent_4>=-62) {
  331.         int prec_bits;
  332.         int num_bits;
  333.  
  334.         unget_bits(1);
  335.         num_bits= -exponent_4;
  336.         prec_bits=LITTLENUM_NUMBER_OF_BITS*precision-(exponent_bits+1+num_bits);
  337.         if(precision==X_PRECISION && exponent_bits==15)
  338.             prec_bits-=LITTLENUM_NUMBER_OF_BITS+1;
  339.  
  340.         if(num_bits>=LITTLENUM_NUMBER_OF_BITS-exponent_bits) {
  341.             /* Bigger than one littlenum */
  342.             num_bits-=(LITTLENUM_NUMBER_OF_BITS-1)-exponent_bits;
  343.             *lp++=word1;
  344.             if(num_bits+exponent_bits+1>=precision*LITTLENUM_NUMBER_OF_BITS) {
  345.                 /* Exponent overflow */
  346.                 make_invalid_floating_point_number(words);
  347.                 return return_value;
  348.             }
  349.             if(precision==X_PRECISION && exponent_bits==15) {
  350.                 *lp++=0;
  351.                 *lp++=0;
  352.                 num_bits-=LITTLENUM_NUMBER_OF_BITS-1;
  353.             }
  354.             while(num_bits>=LITTLENUM_NUMBER_OF_BITS) {
  355.                 num_bits-=LITTLENUM_NUMBER_OF_BITS;
  356.                 *lp++=0;
  357.             }
  358.             if(num_bits)
  359.                 *lp++=next_bits(LITTLENUM_NUMBER_OF_BITS-(num_bits));
  360.         } else {
  361.             if(precision==X_PRECISION && exponent_bits==15) {
  362.                 *lp++=word1;
  363.                 *lp++=0;
  364.                 if(num_bits==LITTLENUM_NUMBER_OF_BITS) {
  365.                     *lp++=0;
  366.                     *lp++=next_bits(LITTLENUM_NUMBER_OF_BITS-1);
  367.                 } else if(num_bits==LITTLENUM_NUMBER_OF_BITS-1)
  368.                     *lp++=0;
  369.                 else
  370.                     *lp++=next_bits(LITTLENUM_NUMBER_OF_BITS-1-num_bits);
  371.                 num_bits=0;
  372.             } else {
  373.                 word1|= next_bits ((LITTLENUM_NUMBER_OF_BITS-1) - (exponent_bits+num_bits));
  374.                 *lp++=word1;
  375.             }
  376.         }
  377.         while(lp<words+precision)
  378.             *lp++=next_bits(LITTLENUM_NUMBER_OF_BITS);
  379.  
  380.         /* Round the mantissa up, but don't change the number */
  381.         if(next_bits(1)) {
  382.             --lp;
  383.             if(prec_bits>LITTLENUM_NUMBER_OF_BITS) {
  384.                 int n = 0;
  385.                 int tmp_bits;
  386.  
  387.                 n=0;
  388.                 tmp_bits=prec_bits;
  389.                 while(tmp_bits>LITTLENUM_NUMBER_OF_BITS) {
  390.                     if(lp[n]!=(LITTLENUM_TYPE)-1)
  391.                         break;
  392.                     --n;
  393.                     tmp_bits-=LITTLENUM_NUMBER_OF_BITS;
  394.                 }
  395.                 if(tmp_bits>LITTLENUM_NUMBER_OF_BITS || (lp[n]&mask[tmp_bits])!=mask[tmp_bits]) {
  396.                     unsigned long int carry;
  397.  
  398.                     for (carry = 1; carry && (lp >= words); lp --) {
  399.                         carry = * lp + carry;
  400.                         * lp = carry;
  401.                         carry >>= LITTLENUM_NUMBER_OF_BITS;
  402.                     }
  403.                 }
  404.             } else if((*lp&mask[prec_bits])!=mask[prec_bits])
  405.                 *lp++;
  406.         }
  407.  
  408.         return return_value;
  409.     } else     if (exponent_4 & ~ mask [exponent_bits]) {
  410.             /*
  411.              * Exponent overflow. Lose immediately.
  412.              */
  413.  
  414.             /*
  415.              * We leave return_value alone: admit we read the
  416.              * number, but return a floating exception
  417.              * because we can't encode the number.
  418.              */
  419.         make_invalid_floating_point_number (words);
  420.         return return_value;
  421.     } else {
  422.         word1 |=  (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS-1) - exponent_bits))
  423.             | next_bits ((LITTLENUM_NUMBER_OF_BITS-1) - exponent_bits);
  424.     }
  425.  
  426.     * lp ++ = word1;
  427.  
  428.     /* X_PRECISION is special: it has 16 bits of zero in the middle,
  429.        followed by a 1 bit. */
  430.     if(exponent_bits==15 && precision==X_PRECISION) {
  431.         *lp++=0;
  432.         *lp++= 1<<(LITTLENUM_NUMBER_OF_BITS)|next_bits(LITTLENUM_NUMBER_OF_BITS-1);
  433.     }
  434.  
  435.     /* The rest of the words are just mantissa bits. */
  436.     while(lp < words + precision)
  437.         *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
  438.  
  439.     if (next_bits (1)) {
  440.         unsigned long int    carry;
  441.             /*
  442.              * Since the NEXT bit is a 1, round UP the mantissa.
  443.              * The cunning design of these hidden-1 floats permits
  444.              * us to let the mantissa overflow into the exponent, and
  445.              * it 'does the right thing'. However, we lose if the
  446.              * highest-order bit of the lowest-order word flips.
  447.              * Is that clear?
  448.              */
  449.  
  450.  
  451. /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  452.     Please allow at least 1 more bit in carry than is in a LITTLENUM.
  453.     We need that extra bit to hold a carry during a LITTLENUM carry
  454.     propagation. Another extra bit (kept 0) will assure us that we
  455.     don't get a sticky sign bit after shifting right, and that
  456.     permits us to propagate the carry without any masking of bits.
  457. #endif */
  458.         for (carry = 1, lp --; carry && (lp >= words); lp --) {
  459.             carry = * lp + carry;
  460.             * lp = carry;
  461.             carry >>= LITTLENUM_NUMBER_OF_BITS;
  462.         }
  463.         if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) ) {
  464.             /* We leave return_value alone: admit we read the
  465.              * number, but return a floating exception
  466.              * because we can't encode the number.
  467.              */
  468.             *words&= ~ (1 << (LITTLENUM_NUMBER_OF_BITS - 1));
  469.             /* make_invalid_floating_point_number (words); */
  470.             /* return return_value; */
  471.         }
  472.     }
  473.     return (return_value);
  474. }
  475.  
  476. /* This routine is a real kludge.  Someone really should do it better, but
  477.    I'm too lazy, and I don't understand this stuff all too well anyway
  478.    (JF)
  479.  */
  480. void
  481. int_to_gen(x)
  482. long x;
  483. {
  484.     char buf[20];
  485.     char *bufp;
  486.  
  487.     sprintf(buf,"%ld",x);
  488.     bufp= &buf[0];
  489.     if(atof_generic(&bufp,".", EXP_CHARS, &generic_floating_point_number))
  490.         as_warn("Error converting number to floating point (Exponent overflow?)");
  491. }
  492.